﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataLibrary;

namespace TicketSystem
{
    public partial class UC_TravelType : UserControl
    {
        public UC_TravelType()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();

        private void LoadData()
        {
            var type = tse.DepartmentTypes.Where(u => true).Select(u => new { u.TypeName, u.Created_at, u.Updated_at, u.Operators.Name }).ToList();
            this.dataGridView1.DataSource = type;
            this.dataGridView1.Columns[0].HeaderText = "业务部门分类名称";
            this.dataGridView1.Columns[1].HeaderText = "信息增加时间";
            this.dataGridView1.Columns[2].HeaderText = "信息修改时间";
            this.dataGridView1.Columns[3].HeaderText = "操作员";
        }
        private void btn_Add_Click(object sender, EventArgs e)
        {
            using (Form_TravelTypeAdd tta = new Form_TravelTypeAdd())
            {
                tta.ShowDialog();
                //这里调用绑定函数
                LoadData();
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            var count = this.dataGridView1.SelectedRows.Count;
            if (count > 0)
              {
                  if (MessageBox.Show("确认删除？", "此删除将一并删除关联的导游", MessageBoxButtons.YesNo) == DialogResult.Yes)
                  {
                      for (int i = 0; i < count; i++)
                      {
                          string typename = this.dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
                          var departmenttype = tse.DepartmentTypes.Where(u => u.TypeName == typename);
                          tse.DepartmentTypes.RemoveRange(departmenttype);
                          var department = tse.Departments.Where(u => u.DepartmentTypes.TypeName == typename);
                          tse.Departments.RemoveRange(department);
                          var guide = tse.Guides.Where(u => u.DepartmentsGuides1.LastOrDefault().Departments.DepartmentTypes.TypeName == typename);
                          tse.Guides.RemoveRange(guide);
                      }
                      int result = tse.SaveChanges();
                      if (result >= 1)
                      {
                          LoadData();
                      }
                      else
                      {
                          MessageBox.Show("删除失败了！");
                      }
                  }
                  else
                  {
                      return;
                  }
    }
            else
            {
                MessageBox.Show("你当前没有选中任何行");
            }
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            int rows = this.dataGridView1.SelectedRows.Count;
            if (rows > 1)
            {
                MessageBox.Show("一次只能修改一行");
            }
            else if (rows < 1)
            {
                MessageBox.Show("你当前没有选中任何行");
            }
            else
            {
                string TypeName = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();

                using (Form_TravelTypeUpdate ttu = new Form_TravelTypeUpdate())
                {
                    ttu.typename = TypeName;
                    ttu.ShowDialog();
                    //这里调用绑定函数
                    LoadData();
                }
            }
        }

        private void UC_TravelType_Load(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
